//
//  MemberManagerViewController.m
//  Leo01
//
//  Created by 张旭 on 15/9/14.
//  Copyright (c) 2015年 张旭. All rights reserved.
//

#import "MemberManagerViewController.h"
#import "AppDelegate.h"
#import "AddSafeAreaTableViewCell.h"
#import "MemberListTableCellTableViewCell.h"
#import "AddContactsViewController.h"
#import "AddSosContactsViewController.h"
#import "ModifyManagerViewController.h"
#import "ContactDetailViewController.h"
#import "SOSContactDetailViewController.h"

#import "SyncManager.h"
#import "DataManager.h"

#import "LeoMsg.h"
#import "Manager.h"

@interface MemberManagerViewController ()
@property (nonatomic,retain) SyncManager *syncManager;
@property (nonatomic,retain) DataManager *dataManager;
@property (nonatomic,retain) NSArray *ContactsArray;
@property (nonatomic,retain) NSArray *ManagersArray;
@property (nonatomic,retain) NSArray *SosContactsArray;

@property (nonatomic,retain) UILabel *leftPoint;
@property (nonatomic,retain) UILabel *rightPoint;

@property (nonatomic,retain) UIView *mainNavView;


@end

@implementation MemberManagerViewController
@synthesize navLeftButton;
@synthesize ManagerButton;
@synthesize ManagerButtonWidth;
@synthesize ManagerButtonUnderLine;
@synthesize ManagerButtonUnderLineWidth;
@synthesize ContactsButton;
@synthesize ContactsButtonWidth;
@synthesize ContactsButtonUnderLine;
@synthesize ContactsButtonUnderLineWidth;
@synthesize SosContactsButton;
@synthesize SosContactsButtonWidth;
@synthesize SosContactsButtonUnderLine;
@synthesize SosContactsButtonUnderLineWidth;

@synthesize tableView;
@synthesize tableViewHeight;
@synthesize addSafeAreaTableViewCell;
@synthesize memberListTableCellTableViewCell;
@synthesize tableArray;

@synthesize leftPoint;
@synthesize rightPoint;
-(void)dealloc
{
    [self removeObservers];
    addSafeAreaTableViewCell=NULL;
    memberListTableCellTableViewCell=NULL;
    tableArray=NULL;
    self.syncManager=NULL;
    self.dataManager=NULL;
    self.ContactsArray=NULL;
    self.ManagersArray=NULL;
    self.SosContactsArray=NULL;
    self.leftPoint=NULL;
    self.rightPoint=NULL;
    self.mainNavView=NULL;
}
-(void)getContactDataFromServer{
    User *user=[User sharedSingleton];
    NSString *userId=user.uid;
    
    Watch *watch=[self.dataManager getActiveWatch:userId];
    NSString *watchId=watch.deviceUid;
    [self.syncManager getWatchContact:watchId completion:^(SyncResult result, NSDictionary *retValue, NSString *description) {
        if (result==SUCESS)
        {
            [self initData];
            tableArray = self.ContactsArray;
            [self.tableView reloadData];
            dispatch_queue_t queue=dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
            if(queue!=NULL)
            {
                dispatch_async(queue, ^{
                    [self.syncManager getContactPhotos:watchId];
                });
            }
        }
        else
        {
            [Tooler showHttpMsg:result description:description msg:@"获取联系人数据失败"];
        }
    }];
}
-(void)registerObservers
{
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(receiveNotification:) name:ContactPHOTOUPDATE object:nil];
}
-(void)removeObservers
{
    [[NSNotificationCenter defaultCenter] removeObserver:self name:ContactPHOTOUPDATE object:nil];
}

- (void)receiveNotification:(NSNotification *)sender {
    dispatch_async(dispatch_get_main_queue(), ^{
        [self initData];
        tableArray = self.ContactsArray;
        [self.tableView reloadData];
    });

}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.extendedLayoutIncludesOpaqueBars = YES;
    self.title=@"";
    
//    if( ([[[UIDevice currentDevice] systemVersion] doubleValue]>=7.0)) {
//        self.edgesForExtendedLayout = UIRectEdgeNone;
//        self.extendedLayoutIncludesOpaqueBars = NO;
//        self.modalPresentationCapturesStatusBarAppearance = NO;
//    }
    
    
    //[self initNavButton];
    [self initNavTitleView];
    [self initColorParam];
    //[self initHeaderButtonLayer];
    //[self initButtonClickActions];
    [self worktable];
    [self initDataProxy];
    tableArray = [[NSMutableArray alloc]init];
    [self getContactDataFromServer];

    isManagerPage = NO;
    isContactsPage = YES;
    isSosContactsPage = NO;
    
    [self initData];
    tableArray = self.ContactsArray;
    [self registerObservers];
    //tableArray=[self combineContactAndSoSContact];
}
//-(NSMutableArray*)combineContactAndSoSContact
//{
//    NSMutableArray *combineArray=[[NSMutableArray alloc]init];
//
//    
//    for (int i=0;i<self.ManagersArray.count;i++)
//    {
//        [combineArray addObject:[self.ManagersArray objectAtIndex:i]];
//    }
//
//    for (int i=0;i<self.ContactsArray.count;i++)
//    {
//        [combineArray addObject:[self.ContactsArray objectAtIndex:i]];
//    }
//    for (int i=0;i<self.SosContactsArray.count;i++)
//    {
//        [combineArray addObject:[self.SosContactsArray objectAtIndex:i]];
//    }
//    return combineArray;
//}

-(void)viewWillDisappear:(BOOL)animated
{
    [self.mainNavView removeFromSuperview];

}
-(void)viewWillAppear:(BOOL)animated{
    
//    isManagerPage = NO;
//    isContactsPage = YES;
//    isSosContactsPage = NO;
    
    tableArray = [[NSMutableArray alloc]init];
    tableArray = self.ContactsArray;
    //tableArray=[self combineContactAndSoSContact];
    [self initDataProxy];
    [self initData];
    
    if (isContactsPage) {
        tableArray = self.ContactsArray;
        //tableArray=[self combineContactAndSoSContact];
    }
    else
    {
        tableArray = self.SosContactsArray;
    }
    [tableView reloadData];
    
    [self.navigationController.view addSubview:self.mainNavView];

}

//**自定义navigation中间
-(void) initNavTitleView
{
    float width=[UIScreen mainScreen].bounds.size.width;
    float buttonWidth=width/4;
    float sizePoint=25;
    
    //self.navBarItem.titleView
    //self.navBarItem.titleView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, width, 40)];
    
    UIView *navCoverView=[[UIView alloc]initWithFrame:CGRectMake(buttonWidth, 20, buttonWidth*2, 44)];
    
    UIButton *leftButton=[[UIButton alloc]initWithFrame:CGRectMake(0, 0, buttonWidth, 44)];
    UIButton *rightButton=[[UIButton alloc]initWithFrame:CGRectMake(buttonWidth, 0, buttonWidth, 44)];
    leftPoint=[[UILabel alloc]initWithFrame:CGRectMake(buttonWidth/2-sizePoint/5, 20, sizePoint, sizePoint)];
    rightPoint=[[UILabel alloc]initWithFrame:CGRectMake(buttonWidth*1.5-sizePoint/5, 20, sizePoint, sizePoint)];
    leftPoint.adjustsFontSizeToFitWidth=YES;    rightPoint.adjustsFontSizeToFitWidth=YES;
    leftPoint.textColor=[UIColor whiteColor];   rightPoint.textColor=[UIColor whiteColor];
    leftPoint.text=@"•";    rightPoint.text=@"•";
    
    leftButton.backgroundColor=[UIColor clearColor];
    rightButton.backgroundColor=[UIColor clearColor];
    leftButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    rightButton.titleLabel.font = [UIFont fontWithName:@"Helvetica-Bold" size:15];
    [leftButton setTitle:@"联系人" forState:UIControlStateNormal];
    [rightButton setTitle:@"紧急联系人" forState:UIControlStateNormal];
    
    leftButton.titleLabel.textColor=[UIColor whiteColor];
    rightButton.titleLabel.textColor=[UIColor whiteColor];
    
    [leftButton addTarget:self action:@selector(ContactsButtonClickAction) forControlEvents:UIControlEventTouchDown];
    
    [rightButton addTarget:self action:@selector(SosContactsButtonClickAction) forControlEvents:UIControlEventTouchDown];
    
    [navCoverView addSubview:leftPoint];
    [navCoverView addSubview:rightPoint];
    [navCoverView addSubview:leftButton];
    [navCoverView addSubview:rightButton];
    //[self.navigationController.view addSubview:navCoverView];
    rightPoint.hidden=YES;
    
    self.mainNavView=navCoverView;
    
}


-(void)initDataProxy{
    self.syncManager=[[SyncManager alloc] init];
    self.dataManager=[[DataManager alloc] init];
}

-(void)initData{
//    self.ContactsArray = [self.dataManager getContactLists:@"3" accountId:@"10"];
//    self.ManagersArray = [self.dataManager getManagerLists:@"3" accountId:@"10"];
//    self.SosContactsArray = [self.dataManager getSOSContactLists:@"3" accountId:@"10"];
    
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    myDelegate.ManagerDetailsDataArray = [[NSMutableArray alloc]init];
    myDelegate.ContactsDetailsDataArray = [[NSMutableArray alloc]init];
    myDelegate.SosContactsDetailsDataArray = [[NSMutableArray alloc]init];
    
    //[myDelegate.ManagerDetailsDataArray addObject:dicOne];
    User *user=[User sharedSingleton];
    NSString *userId=user.uid;
    
    
    
    
    Watch *watch=[self.dataManager getActiveWatch:userId];
    NSString *watchId=watch.deviceUid;
    
    self.ContactsArray = [self.dataManager getContactLists:watchId accountId:userId];
    self.ManagersArray = [self.dataManager getManagerLists:watchId accountId:userId];
    self.SosContactsArray = [self.dataManager getSOSContactLists:watchId accountId:userId];
    
    int k=self.ContactsArray.count-[self countOridinaryContacts];
    
    //增加watch到ContactArray中
    NSMutableArray *newContactsArray=[[NSMutableArray alloc]initWithArray:self.ContactsArray];
    NSArray *watchContacts=[self.dataManager getWatchContacts:watchId role:watch.role];
    if (watchContacts!=NULL){
        for (int j=0;j<watchContacts.count;j++){
            //只有作为主管理员的手表才可以加入
            
            [newContactsArray insertObject:[watchContacts objectAtIndex:j] atIndex:k+j];
        }
    }
    
    
    self.ContactsArray=newContactsArray;
    //self.ContactsArray=[self reOrderContactArray:self.ContactsArray];

    
    NSLog(@"Contacts count = %lu,Managers count = %lu,SosContacts count = %lu",(unsigned long)self.ContactsArray.count,(unsigned long)self.ManagersArray.count,self.SosContactsArray.count);
}

- (void)initNavButton {
    [navLeftButton setTarget:self];
    [navLeftButton setAction:@selector(back)];
}

- (void)back{
    [self.navigationController popViewControllerAnimated:YES];
    //[self dismissViewControllerAnimated:YES completion:nil];
}

- (void)initColorParam{
    ButtonTextNormalcolor = [UIColor colorWithRed:142.0/255 green:142.0/255 blue:147.0/255 alpha:1];
    ButtonTextHighLightcolor = [UIColor colorWithRed:71.0/255 green:178.0/255 blue:247.0/255 alpha:1];
}

//init the width for all size screen
- (void)initHeaderButtonLayer{
    CGRect rect = [UIScreen mainScreen].bounds;
    ManagerButtonWidth.constant = (rect.size.width)/3;
    ManagerButtonUnderLineWidth.constant = (rect.size.width)/3;
    
    ContactsButtonWidth.constant = (rect.size.width)/3;
    ContactsButtonUnderLineWidth.constant = (rect.size.width)/3;
    
    SosContactsButtonWidth.constant = (rect.size.width)/3;
    SosContactsButtonUnderLineWidth.constant = (rect.size.width)/3;
    
}



- (void)initButtonClickActions{
    // set init state for the first enter
    ManagerButtonUnderLine.hidden = NO;
    ContactsButtonUnderLine.hidden = YES;
    SosContactsButtonUnderLine.hidden = YES;
    [ManagerButton setTitleColor:ButtonTextHighLightcolor forState:UIControlStateNormal];
    //add actions
    [ManagerButton addTarget:self action:@selector(ManagerButtonClickAction) forControlEvents:UIControlEventTouchDown];
    
    [ContactsButton addTarget:self action:@selector(ContactsButtonClickAction) forControlEvents:UIControlEventTouchDown];
    
    [SosContactsButton addTarget:self action:@selector(SosContactsButtonClickAction) forControlEvents:UIControlEventTouchDown];
}

- (void)ManagerButtonClickAction{
    isManagerPage = YES;
    isContactsPage = NO;
    isSosContactsPage = NO;
//    ManagerButtonUnderLine.hidden = NO;
//    ContactsButtonUnderLine.hidden = YES;
//    SosContactsButtonUnderLine.hidden = YES;
//    [ManagerButton setTitleColor:ButtonTextHighLightcolor forState:UIControlStateNormal];
//    [ContactsButton setTitleColor: ButtonTextNormalcolor forState:UIControlStateNormal];
//    [SosContactsButton setTitleColor: ButtonTextNormalcolor forState:UIControlStateNormal];
//    
    //reload data
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    //test data
    //tableArray = myDelegate.ManagerDetailsDataArray;
    tableArray = self.ManagersArray;
    [tableView reloadData];
}

- (void)ContactsButtonClickAction{
    rightPoint.hidden=YES;
    leftPoint.hidden=NO;

    isManagerPage = NO;
    isContactsPage = YES;
    isSosContactsPage = NO;
//    ManagerButtonUnderLine.hidden = YES;
//    ContactsButtonUnderLine.hidden = NO;
//    SosContactsButtonUnderLine.hidden = YES;
//    [ManagerButton setTitleColor: ButtonTextNormalcolor forState:UIControlStateNormal];
//    [ContactsButton setTitleColor: ButtonTextHighLightcolor forState:UIControlStateNormal];
//    [SosContactsButton setTitleColor:ButtonTextNormalcolor forState:UIControlStateNormal];
    
    //reload data
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    //test data
    //tableArray = myDelegate.ContactsDetailsDataArray;
    tableArray = self.ContactsArray;
    //tableArray=[self combineContactAndSoSContact];
    [tableView reloadData];
}

- (void)SosContactsButtonClickAction{
    
    rightPoint.hidden=NO;
    leftPoint.hidden=YES;
    
    isManagerPage = NO;
    isContactsPage = NO;
    isSosContactsPage = YES;
//    ManagerButtonUnderLine.hidden = YES;
//    ContactsButtonUnderLine.hidden = YES;
//    SosContactsButtonUnderLine.hidden = NO;
//    [ManagerButton setTitleColor: ButtonTextNormalcolor forState:UIControlStateNormal];
//    [ContactsButton setTitleColor: ButtonTextNormalcolor forState:UIControlStateNormal];
//    [SosContactsButton setTitleColor:ButtonTextHighLightcolor forState:UIControlStateNormal];
    
    //reload data
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    //test data
    //tableArray = myDelegate.SosContactsDetailsDataArray;
    tableArray = self.SosContactsArray;
    [tableView reloadData];
}

-(void)worktable{
    self.automaticallyAdjustsScrollViewInsets=NO;
    tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    tableView.rowHeight = UITableViewAutomaticDimension;
    tableView.delegate=self;
    tableView.dataSource=self;
    
    UINib *cellMember = [UINib nibWithNibName:@"MemberListTableCellTableViewCell" bundle:nil];
    [tableView registerNib:cellMember forCellReuseIdentifier:@"memberlisttablecell"];
    self.memberListTableCellTableViewCell  = [tableView dequeueReusableCellWithIdentifier:@"memberlisttablecell"];
    
    UINib *cellAdd = [UINib nibWithNibName:@"AddSafeAreaTableViewCell" bundle:nil];
    [tableView registerNib:cellAdd forCellReuseIdentifier:@"addSafeAreaCell"];
    self.addSafeAreaTableViewCell  = [tableView dequeueReusableCellWithIdentifier:@"addSafeAreaCell"];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    if(self.view.window==nil)
    {
        self.view=nil;
    }
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

//计算Contact中普通联系人的个数
-(int)countOridinaryContacts{
    int k=0;
    for (int i=0;i<self.ContactsArray.count;i++){
        if ([[self.ContactsArray objectAtIndex:i] isKindOfClass:[Contact class]]){
            k=k+1;
        };

    }
    return k;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    CGRect rect = [UIScreen mainScreen].bounds;
    if (isManagerPage) {
        if (80 * (tableArray.count) < rect.size.height) {
            tableViewHeight.constant = 80 * (tableArray.count);
        }
        else
            tableViewHeight.constant = rect.size.height - 80;
        return tableArray.count;
    }
    else{
        AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
        if(mydelegate.isAdmin==NO)
        {
            if (80 * tableArray.count< rect.size.height) {
                tableViewHeight.constant = 80 * tableArray.count;
            }
            else
                tableViewHeight.constant = rect.size.height - 80;
                        return tableArray.count;
        }
        else
        {
            
            if (80 * (tableArray.count+1) < rect.size.height) {
                tableViewHeight.constant = 80 * (tableArray.count+1);
            }
            else
                tableViewHeight.constant = rect.size.height - 80;
            if (tableArray.count>=50) //最大数量限制
            {
               return 50;
            }
//            //if ([self.ContactsArray count]==40){
//            if ([self countOridinaryContacts]==40){
//                return 40;
//            }
            //38
            if (([self countOridinaryContacts]<38)&&(isContactsPage))
            return tableArray.count+1;
            if ((tableArray.count<3)&&(isSosContactsPage))
            return tableArray.count+1;

            
            return tableArray.count;
        }
        
    }
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (isManagerPage) {
        MemberListTableCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"memberlisttablecell"];
        cell.navController=self.navigationController;
        [cell setIsManager: YES];
        [cell setFromContact:YES];
        cell = [cell getandSetDataWithManager:[tableArray objectAtIndex:indexPath.row] Type:2];
        cell=[self addCellLine:cell forIndex:indexPath.row];
        return cell;
    }
    else{
        if((indexPath.row == tableArray.count)){
            AddSafeAreaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"addSafeAreaCell"];
            cell.navController=self.navigationController;
            cell=[self addCellLine:cell forIndex:indexPath.row];
            return cell;
        }
        MemberListTableCellTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"memberlisttablecell"];
        cell.navController=self.navigationController;
        [cell setIsManager: NO];
        [cell setFromContact:YES];
        //cell.selectionStyle = UITableViewCellSelectionStyleNone;
        //if ([originalObject valueForKey:@"manager_id"]!=nil)
        if ([[tableArray objectAtIndex:indexPath.row] isKindOfClass:[Manager class]])
        {
            
            cell = [cell getandSetDataWithManager:[tableArray objectAtIndex:indexPath.row]Type:2];
        }
        if ([[tableArray objectAtIndex:indexPath.row] isKindOfClass:[Contact class]]){
            cell = [cell getandSetDataWithContact:[tableArray objectAtIndex:indexPath.row]];
        };
        if ([[tableArray objectAtIndex:indexPath.row] isKindOfClass:[Watch class]]){
            cell = [cell getandSetDataWithWatch:[tableArray objectAtIndex:indexPath.row]];
        }
        cell=[self addCellLine:cell forIndex:indexPath.row];
        AppDelegate *mydelegate=[[UIApplication sharedApplication] delegate];
        if(mydelegate!=NULL&&mydelegate.isAdmin==NO)
        {
            [cell setAccessoryType:UITableViewCellAccessoryNone];
        }
        else
        {
            [cell setAccessoryType:UITableViewCellAccessoryDisclosureIndicator];
        }
        return cell;
    }
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    if (isContactsPage) {
        if(indexPath.row == tableArray.count){
            AddContactsViewController *addContactsViewController = [[AddContactsViewController alloc]init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:addContactsViewController animated:YES];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
        else
        {
            if(myDelegate!=NULL&&myDelegate.isAdmin==NO)
                return;
            ContactDetailViewController *contactDetailViewController=[[ContactDetailViewController alloc]init];
            if ([[tableArray objectAtIndex:indexPath.row] isKindOfClass:[Manager class]]){
                contactDetailViewController.manager=[tableArray objectAtIndex:indexPath.row];
            }
            if ([[tableArray objectAtIndex:indexPath.row] isKindOfClass:[Contact class]]){
                contactDetailViewController.contact=[tableArray objectAtIndex:indexPath.row];
            }
            if ([[tableArray objectAtIndex:indexPath.row] isKindOfClass:[Watch class]]){
                contactDetailViewController.watchContact=[tableArray objectAtIndex:indexPath.row];
            }

            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:contactDetailViewController animated:YES];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }

    }else if (isSosContactsPage) {
        if(indexPath.row == tableArray.count){
            AddSosContactsViewController *addSosContactsViewController = [[AddSosContactsViewController alloc]init];
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:addSosContactsViewController animated:YES];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];
        }
        else
        {
            if(myDelegate!=NULL&&myDelegate.isAdmin==NO)
                return;
            SOSContactDetailViewController *sosContactViewController=
            [[SOSContactDetailViewController alloc]init];
            if ([[tableArray objectAtIndex:indexPath.row] isKindOfClass:[Manager class]])
            {
                sosContactViewController.manager=[tableArray objectAtIndex:indexPath.row];
            }
            else
            {
                sosContactViewController.contact=[tableArray objectAtIndex:indexPath.row];
            }
            self.hidesBottomBarWhenPushed = YES;
            [self.navigationController pushViewController:sosContactViewController animated:YES];
            [tableView deselectRowAtIndexPath:indexPath animated:YES];

        }
    }else if (isManagerPage) {
        ModifyManagerViewController *modifyManagerViewController = [[ModifyManagerViewController alloc]init];
        self.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:modifyManagerViewController animated:YES];
    }
    
}

-(UITableViewCell *)addCellLine:(UITableViewCell *)cell forIndex:(int)index
{
    UILabel *line;
    bool islastLine=NO;
    if (index==tableArray.count)
    {
        islastLine=YES;
        
    }
    if (([self countOridinaryContacts]>=38)&&(isContactsPage)){
        if (index==tableArray.count-1){
            islastLine=YES;
        }

    }
    if ((self.SosContactsArray.count>=3)&&(isSosContactsPage)){
        if (index==tableArray.count-1){
            islastLine=YES;
        }
    }
    if (islastLine){
        NSLog(@"有最后一条！");
        line=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.frame.size.height-1, [UIScreen mainScreen].bounds.size.width-0, 1)];
        line.backgroundColor=[UIColor colorWithRed:226/255.0 green:226/255.0 blue:228/255.0 alpha:1.0];
        [cell addSubview:line];
        return cell;

    }

    AppDelegate *myDelegate = [[UIApplication sharedApplication] delegate];
    if((myDelegate.isAdmin==NO)&&(index==tableArray.count-1)){
        line=[[UILabel alloc]initWithFrame:CGRectMake(0, cell.frame.size.height-1, [UIScreen mainScreen].bounds.size.width-0, 1)];
    }
    else{
        line=[[UILabel alloc]initWithFrame:CGRectMake(20, cell.frame.size.height-1, [UIScreen mainScreen].bounds.size.width-20, 1)];
    }
    line.backgroundColor=[UIColor colorWithRed:226/255.0 green:226/255.0 blue:228/255.0 alpha:1.0];
    
    [cell addSubview:line];
    
    return cell;
}
//***排序代碼
//-(NSArray *)reOrderContactArray:(NSArray *)contacts{
//    
//    NSMutableArray *newContacts=[[NSMutableArray alloc]initWithArray:contacts];
//    
//    int loc=0;//普通联系人起始位置
//    for (int i=0;i<newContacts.count;i++){
//        if ([[newContacts objectAtIndex:i] isKindOfClass:[Contact class]]){
//            loc=i;
//            break;
//        }
//    }
//    
//    
//    for (int j=loc;j<newContacts.count;j++){
//        int k=0; double mink=9999999999;
//        for (int i=j;i<newContacts.count;i++){
//            if ([[newContacts objectAtIndex:i] isKindOfClass:[Contact class]]){
//                Contact *person=[newContacts objectAtIndex:i];
//                double x=[person.update_time doubleValue];
//                if (mink>x){mink=x;k=i;}
//            }
//        }
//        Contact *middle=[newContacts objectAtIndex:k];
//        [newContacts replaceObjectAtIndex:k withObject:[newContacts objectAtIndex:j]];
//        [newContacts replaceObjectAtIndex:j withObject:middle];
//    }
//    
//    return newContacts;
//}


@end
